package MainPackage;

import java.util.ArrayList;
import java.util.List;

import org.jcp.xml.dsig.internal.dom.Utils;

public class TranslateFile {
	private String currentFile;
	private String currentLine;
	private boolean debugMode;
	private List<String> depthEndString = new ArrayList<String>();
	private String[] validModifiers = new String[] { "public", "private",
			"static", "abstract", "protected" };

	public String pullBottomDepthString() {
		String returnString = depthEndString.get(depthEndString.size() - 1);
		depthEndString.remove(depthEndString.size() - 1);
		return returnString;
	}

	public boolean checkIfModifer(String word) {
		for (String mod : validModifiers) {
			if (mod.equals(word)) {
				return true;
			}
		}
		return false;
	}

	public String generalStringConvert(String javaString) {
		javaString = javaString.replace(";", "");
		javaString = javaString.replace("{", "");
		javaString = javaString.replace("}", "");
		javaString = javaString.replace("!", "not ");
		javaString = javaString.replace("Native.", "");
		javaString = javaString.replace("int ", "integer ");
		javaString = javaString.replace("double ", "real ");
		javaString = javaString.replace("String ", "string ");
		javaString = javaString.replace("protected ", "public ");
		javaString = javaString.replace(".equals", " == ");
		return javaString;
	}

	public String translateClassString(String javaString) {
		depthEndString.add("endclass");

		return javaString.replace("{", "");
	}

	public String translateLocalVariableString(String javaString) {
		javaString = generalStringConvert(javaString);
		javaString = "local " + javaString;

		return javaString;
	}

	public String translateIfStatement(String javaString) {
		if(!javaString.contains("else if")) {
			depthEndString.add("endif");
		}
		javaString = javaString.replace("if(", "if (");
		javaString = javaString.replace("else if", "elseif");
		javaString = generalStringConvert(javaString);
		javaString = javaString + " then";

		return javaString;
	}

	public String translateCallStatement(String javaString) {
		javaString = generalStringConvert(javaString);
		javaString = "call " + javaString;
		return javaString;
	}

	public String translateMethodLine(String javaString) {
		depthEndString.add("endmethod");
		javaString = generalStringConvert(javaString);
		javaString = javaString.replace("void", "nothing");
		
		String finalString = "";
		javaString = javaString.replace("(", " takes ");
		javaString = javaString.replace(")", " returns ");

		String[] words = javaString.split(" ");
		
		int x = 0;
		while(checkIfModifer(words[x])){
			finalString = finalString + words[x++] + " ";
		}
		
		String returnType = words[x];
		words[x] = "method";
		
		while(x < words.length){
			finalString = finalString + words[x++] + " ";
		}
		
		finalString = finalString + returnType;

		return finalString;
	}
	
	public String translateReturnStatement(String javaString){
		return generalStringConvert(javaString);
	}

	public boolean isDebugMode() {
		return debugMode;
	}

	public void setDebugMode(boolean debugMode) {
		this.debugMode = debugMode;
	}

	public String getCurrentFile() {
		return currentFile;
	}

	public void setCurrentFile(String currentFile) {
		this.currentFile = currentFile;
	}

	public String getCurrentLine() {
		return currentLine;
	}

	public void setCurrentLine(String currentLine) {
		this.currentLine = currentLine;
	}

	public String translateAssignment(String javaString) {
		javaString = generalStringConvert(javaString);
		return "set " + javaString;
	}

	public String translateElseStatement(String javaString) {
		return generalStringConvert(javaString);
	}

}
